package com.pengheng.handler;

import com.pengheng.pojo.Employee;
import com.pengheng.pojo.User;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


/**
 * @remark
 * @Author pengheng
 * @date 2021/5/8 10:43
 */
@RabbitListener(queues = {
        "hello-java-queue"
})
@Component
public class MessageHandler {

    @RabbitHandler
    public void receiveMessage(Message message, User user, Channel channel) {
        System.out.println("接收到的消息内容" + message + "用户信息" + user.toString());
        /**
         * @param deliveryTag 交货标签
         * @param multiple 是否批量签收 false 挨个确认
         */
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            if(deliveryTag%2==0) {
                channel.basicAck(deliveryTag, false);
            }else{
//                channel.basicReject(deliveryTag,true);
                //拒绝重新丢回队列
                channel.basicNack(deliveryTag,false,true);
            }
        } catch (Exception e) {
        }
    }

    @RabbitHandler
    public void receiveMessage(Message message, Employee employee, Channel channel) {
        System.out.println("接收到的消息内容" + message + "打工仔信息" + employee.toString());
    }
}
